// pages/addclient/addclient.js
import { getOrderInfo,createOrder,editOrder,formatDate,toTemp} from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDate:false,
    showPro:false,
    showMd:false,
    minHour:8,
    maxHour:18,
    minDate: new Date().getTime(),
    currentDate: '',
    projects: [],
    shops: [],
    condition:{},
    autosize:{
      maxHeight: 125, minHeight: 60
    },
    date:'',
    mendian:'',
    project:'',
    remark:'',
    orderId:'',
    currentId:''
  },

  onDisplayDate() {
    this.setData({ showDate: true });
  },
  onCloseDate() {
    this.setData({ showDate: false });
  },
  onConfirm(event) {
    console.log(event)
    this.setData({
      showDate: false,
      date: formatDate(event.detail),
    });
  },

  onDisplayPro() {
    this.setData({ showPro: true });
  },
  onClosePro() {
    this.setData({ showPro: false });
  },

  onChangePro(event) {
    this.setData({
      showPro: false,
      project: event.detail.value,
    });
  },

  onDisplayMd() {
    this.setData({ showMd: true });
  },
  onCloseMd() {
    this.setData({ showMd: false });
  },

  onChangeMd(event) {
    this.setData({
      showMd: false,
      mendian: event.detail.value,
    });
  },
  findId(name,data){
    let id=''
    data.forEach(item => {
      if(item.label==name){
        id= item.value;
      }
    });
    return id;
  },
  formSubmit(e){
    if(!this.data.date){
      wx.showToast({
        title: '请选择预约日期',
        icon: 'error',
        duration: 2000
      })
    }else if(!this.data.project){
      wx.showToast({
        title: '请选择项目',
        icon: 'error',
        duration: 2000
      })
    }else if(!this.data.mendian){
      wx.showToast({
        title: '请选择门店',
        icon: 'error',
        duration: 2000
      })
    }else{
      let params={  
        patient_id:this.data.currentId,     
        order_time:this.data.date,
        treatment_project_id:this.findId(this.data.project,this.data.condition.treatment_project),
        shop_id:this.findId(this.data.mendian,this.data.condition.shop),
        remark:e.detail.value.remark
      }
      if(this.data.orderId>0){        
        params.id=this.data.orderId
        editOrder(params).then(res=>{
          let pages=getCurrentPages()
          let beforePage=pages[pages.length-2]
          let page2=pages[pages.length-3]  
          beforePage.getData() 
          page2.refresh()  
          wx.navigateBack({
            data:1
          })   
        })
      }else{        
        createOrder(params).then(res=>{
          let pages=getCurrentPages()
          let beforePage=pages[pages.length-2]
          let page2=pages[pages.length-3]  
          beforePage.getData() 
          page2.refresh()  
          wx.navigateBack({
            data:1
          }) 
        })
      }
      
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  changeData(data){
    let arr=[]
    data.forEach(item => {
      arr.push(item.label)
    });
    return arr;
  },
  onLoad(options) {
    this.setData({
      orderId:options.orderId,
      currentId:options.id
    })
    getOrderInfo({id:this.data.orderId}).then(res=>{
      this.setData({        
        condition:res.data.condition,
        projects:this.changeData(res.data.condition.treatment_project),
        shops:this.changeData(res.data.condition.shop),
        date:res.data.order_time?res.data.order_time:'',
        currentDate:toTemp(res.data.order_time),
        mendian:res.data.shop_name?res.data.shop_name:'',
        project:res.data.treatment_project_name?res.data.treatment_project_name:'',
        remark:res.data.remark?res.data.remark:'',
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})